package com.liqi.web.controller.monitor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liqi.common.annotation.DataScope;
import com.liqi.common.core.controller.BaseController;
import com.liqi.common.core.domain.R;
import com.liqi.common.core.page.TableDataInfo;
import com.liqi.monitor.domain.DvUpdateList;
import com.liqi.monitor.domain.DvUpdatePlan;
import com.liqi.monitor.domain.dto.DeviceInfoDto;
import com.liqi.monitor.domain.dto.PlanListDto;
import com.liqi.monitor.domain.dto.UpdatePlanDto;
import com.liqi.monitor.module.DvUpdatePlanModule;
import com.liqi.monitor.service.DvUpdateListService;
import com.liqi.monitor.service.DvUpdatePlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author: lgh
 * @Date: 2022-10-27-10:56
 * @Description: 更新计划
 */
@RestController
@RequestMapping("/monitor/updatePlan")
@Api(tags = "更新计划接口")
@Validated
public class DvUpdatePlanController extends BaseController {
    @Autowired
    private DvUpdatePlanService dvUpdatePlanService;

    @Autowired
    private DvUpdateListService dvUpdateListService;


    @GetMapping("/getModuleAndDrive")
    @ApiOperation("根据device-code 获取对应的模块和驱动")
//    @PreAuthorize("@ss.hasPermi('monitor:updatePlan:getModuleAndDrive')")
    public TableDataInfo getModuleAndDrive(@ApiParam(name = "deviceCode",value = "device编码",required = true) @RequestParam("deviceCode") String deviceCode){
        return getDataTable(dvUpdatePlanService.getModuleAndDrive(deviceCode));
    }

    @PostMapping("/createUpdatePlan")
    @ApiOperation("新建更新计划")
//    @PreAuthorize("@ss.hasPermi('monitor:updatePlan:createUpdatePlan')")
    public R createUpdatePlan(@Valid @RequestBody List<UpdatePlanDto> list){
        dvUpdatePlanService.createUpdatePlan(list);
        return R.ok("更新计划创建成功");
    }


    @PostMapping("/planList")
    @ApiOperation("更新计划列表")
    // todo 这里进行数据过滤
    @DataScope(deptAlias = "d",userAlias = "u", regionAlias = "r", groupAlias = "g")
//    @PreAuthorize("@ss.hasPermi('monitor:updatePlan:planList')")
    public IPage<DvUpdatePlanModule> planList(@RequestBody PlanListDto dto) {
        Page<DvUpdatePlanModule> page = new Page<>(dto.getCurrent(), dto.getSize());
        List<DvUpdatePlanModule> dvUpdatePlans = dvUpdatePlanService.planList(page,dto);
        page.setRecords(dvUpdatePlans);
        return page;
    }

    @GetMapping("/planDetailed")
    @ApiOperation("计划详细信息")
//    @PreAuthorize("@ss.hasPermi('monitor:updatePlan:planDetailed')")
    public R planDetailed(@ApiParam(name = "planId",value = "计划id",required = true) @RequestParam("planId") String planId
            ,@ApiParam(name = "deviceId",value = "deviceId",required = true)@RequestParam("deviceId")String deviceId) {
        QueryWrapper<DvUpdateList> qw = new QueryWrapper<>();
        qw.eq("plan_id", planId).eq("device_id", deviceId);
        List<DvUpdateList> list = dvUpdateListService.list(qw);
        return R.ok(list);
    }

    @PostMapping("/stopUpdateSoftware")
    @ApiOperation("终止计划接口")
//    @PreAuthorize("@ss.hasPermi('monitor:updatePlan:stopUpdateSoftware')")
    public R stopUpdateSoftware(@Valid @RequestBody List<DeviceInfoDto> deviceInfoDto) {
        String messageCode = dvUpdatePlanService.stopUpdateSoftware(deviceInfoDto);
        return R.ok(messageCode);
    }

    @GetMapping("/stopUpdatePolling")
    @ApiOperation("终止计划轮询接口")
    public R stopUpdatePolling(@ApiParam(value = "消息编码",required = true)@RequestParam("messageCode")String messageCode){
        Integer msgCode = dvUpdatePlanService.getStatusBymessageCode(messageCode);
        return R.ok(msgCode);
    }

}
